package control;
  
import view.StatisticChartView;
import model.Share;
import model.Time;
import model.Account;
import view.AccountView;
import java.awt.*;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import model.StockData;
  
/**
 * 
 * @author diego
 */
public class StockManager extends JFrame {
     
    private StockData modelAccount;
    private StockData modelShare;
    private StockData modelTime;
    private JTable tableAccount;
    private JTable tableShare;
    private JTable tableTime;        
    private static final Logger logger = Logger.getLogger(StockManager.class.getName());
    /*
    public static void main(String args[]) {
       
        try {
            //UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
            UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
        } catch (UnsupportedLookAndFeelException ex) {
            ex.printStackTrace();
        } catch (IllegalAccessException ex) {
            ex.printStackTrace();
        } catch (InstantiationException ex) {
            ex.printStackTrace();
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        }
         
        UIManager.put("swing.boldMetal", Boolean.FALSE);
        
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
             StockManager frame=   new StockManager();
                frame.pack();
                frame.setVisible(true);

            }
        });
 
    }
*/
    public StockManager()  {
        super( "Stock Manager : Our Time "+ new Date());
        
        try{
            loadConfiguration();             
            logger.log(Level.INFO, "carregou configurações com sucesso;"); 
        }catch(Exception e){
            logger.log(Level.SEVERE, null, e);      
        }
        Account account = new Account( tableAccount );     
        Share share = new Share( tableShare );
        Time time = new Time( tableTime );
         
        JPanel jpAccount = createAccountPanel( account, share, time );
          
        Container contentPane = getContentPane();
        contentPane.add( jpAccount );
        //contentPane.add( demo );
         
        final DynamicChartController chartController = new DynamicChartController(account, share, time);
        final StatisticChartView chartViewer  = new StatisticChartView(account, share, time);
        
        chartViewer.pack();
        chartController.pack();
        //RefineryUtilities.
        //variationChart.setAlwaysOnTop(true);
        chartViewer.setLocation(0, 500);  
        chartController.setLocation(0, 0); 
        
        chartViewer.setVisible(true);
        chartController.setVisible(true);        
        setLocation(600,0);
        setSize( 500, 600  );
    }  
       
    public void loadConfiguration(){
          
        
        this.modelShare = new StockData("resources/config/share.txt");        
        this.tableShare = new JTable();
        this.tableShare.setModel(modelShare);
        //this.tableShare.createDefaultColumnsFromModel();
        
        this.modelTime = new StockData("resources/config/time.txt");       
        this.tableTime = new JTable();
        this.tableTime.setModel(modelTime);
        this.tableTime.createDefaultColumnsFromModel();
        
        
        this.modelAccount = new StockData("resources/config/account.txt");
        this.tableAccount = new JTable();
        this.tableAccount.setModel(modelAccount);
        this.tableAccount.createDefaultColumnsFromModel();
        /*
        Object[][] fopShare = new Object[tableShare.getModel().getRowCount()][tableShare.getModel().getColumnCount()];
          for(int x=0;x<tableShare.getModel().getRowCount();x++){
              for(int y=0;y<tableShare.getModel().getColumnCount();y++){
              //System.out.print(tableShare.getModel().getValueAt(x, y)+ " ");
              fopShare[x][y]= tableShare.getModel().getValueAt(x, y);
              //System.out.print(fopShare[x][y]+ " ");
        }  
              //System.out.println();
        }
          */ 
    }
 
    private JPanel createAccountPanel(Account account, Share share, Time time ) {
        
       
        AccountView jpAccount = new AccountView( account );   
        AccountController jpTransaction = new AccountController( account, share, time );
        ShareController jpShareController = new ShareController(account, share, time);
        //jpShareController.setPreferredSize(new Dimension(500,100));
        
        
        JPanel jpLayout = new JPanel();
        GridLayout experimentLayout = new GridLayout(0,1);
	jpLayout.setLayout(experimentLayout);
        jpLayout.add(jpShareController);  
        //jpLayout.add(jpShare);      
        jpLayout.add(jpTransaction);              
        jpLayout.add(jpAccount);                  
        //jpLayout.add(timerController);
        
        return jpLayout;
    }// </editor-fold>
     
      
    
}
